Of the three, only the ITIMER_REAL, which measures elapsed time, is useful for real-time processes. The signal it generates, SIGALRM, is always delivered as soon as possible. (Other signals are not delivered until a scheduling interval occurs; see "Signal Delivery and Latency".)
Note: Interval timers are not normally used with the Frame Scheduler. See "Using Timers with the Frame Scheduler"
Example 5-1 : Timer Initialization
usema_t * alarmSema; /* initialized elsewhere */ void uponSigalrm() { usvsema(alarmSema); /* count a period */ } int setUpTimer(long periodInMicrosec) { struct sigaction alarmActor = {SA_RESTART,uponSigalrm,0}; struct itimerval period = {{0, 0}, {0, 0}}; if (sigaction(SIGALRM, & alarmActor, NULL)) { perror("sigaction"); return -1; } period.it_interval.tv_usec = periodInMicrosec; period.it_value.tv_usec = periodInMicrosec; if (setitimer(ITIMER_REAL, &period, NULL)) { perror("setitimer"); return -1; } return 0; /* indicate success */ }The example begins by defining a signal-handling function, uponSigalrm(). This handler performs the V (count-up, revive) operation on a semaphore. When the main process has completed its work for one interval and is ready to wait until the next interval, it will perform the P (count-down, deplete) operation on the same semaphore. (This is one of many possibilities for interaction between the signal handler and the main process. For an example of another method based on sigsuspend(), see "Interprocess Communication" in Appendix A.)
The periodic timer is initialized in the function setUpTimer(). It establishes uponSigalrm() as the signal handler. Then it initializes the itimer, using a period passed as an argument.